package com.example.springcloud.controller;

import com.example.springcloud.pojo.Product;
import com.example.springcloud.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: trtan
 * @date: 2021-06-13 07:39
 **/
@Controller
@RefreshScope
public class ProductController {
    @Value("${version}")
    String version;

    @Value("${server.port}")
    String port;

    @Autowired
    ProductService productService;

    @RequestMapping("/products")
    public String products(Model model) {
        List<Product> allProducts = productService.getAllProducts();
        model.addAttribute("products", allProducts);
        model.addAttribute("version", version);
        model.addAttribute("port", "视图端口 " + port);
        return "products";
    }

    @RequestMapping("/version")
    @ResponseBody
    public String getVersion() {
        return version;
    }
}
